package com.ly10111.customedittext.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.ly10111.customedittext.R;
import com.ly10111.customedittext.view.KeyboardView;

public class MainActivity extends AppCompatActivity {

    private EditText et;
    private KeyboardView kv;
    private RadioGroup rg;
    private int inputType = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        et = findViewById(R.id.customEditText);
        rg = findViewById(R.id.rg);
        kv = findViewById(R.id.kv);
        et.setShowSoftInputOnFocus(false);
        et.setOnClickListener(v -> {
            if (kv.getVisibility() == View.GONE && inputType > 0) {
                kv.setVisibility(View.VISIBLE);
                kv.setInputType(inputType, et);
            }
        });
        et.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus && kv.getVisibility() == View.VISIBLE) kv.setVisibility(View.GONE);
        });
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            kv.setVisibility(View.GONE);
            if (checkedId == R.id.zy) inputType = KeyboardView.inputType_zy;
            else inputType = KeyboardView.inputType_py;
        });
    }

    @Override
    public void onBackPressed() {
        if (kv.getVisibility() == View.VISIBLE) {
            kv.setVisibility(View.GONE);
            return;
        }
        super.onBackPressed();
    }
}
